<%@ page import="com.note.pojo.notePageShow" %>
<%@ page import="java.util.List" %>
<%@ page import="com.note.pojo.Notetype" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<%@include file="/jsp/common/head.jsp"%>
<div class="right">
    <div class="location">
        <strong>你现在所在的位置是:</strong>
        <span>博客管理页面</span>
    </div>
    <!--用户-->
    <%
        List<Notetype> typeList = (List<Notetype>) request.getAttribute("typeList");
    %>
    <div>
        <input type="text" id="title" style="height: 30px">
        <button style="height: 30px" id="btn1">查询</button>
        <select style="height: 30px;" id="type">
            <option value="-1">...</option>
            <%
                for (int i = 0; i < typeList.size(); i++) {
            %>
            <option value="<%=typeList.get(i).getTypeid()%>"><%=typeList.get(i).getTypename()%></option>
            <%
                }
            %>
        </select>

    </div>
    <table class="providerTable" cellpadding="0" cellspacing="0">
        <tr class="firstTr">
            <td>编号</td>
            <td>标题</td>
            <td>类型</td>
            <td>作者</td>
            <td>时间</td>
            <td>操作</td>
        </tr>
        <tbody id="data">
        <%
            com.note.pojo.page page1 = (com.note.pojo.page) request.getAttribute("page");
            for (notePageShow note : page1.getNotes()) {
        %>
        <tr>
            <td><%=note.getNoteId()%></td>
            <td><a href="${pageContext.request.contextPath}/noteView?noteId=<%=note.getNoteId()%>"><%=note.getTitle()%></a></td>
            <td><%=note.getTypename()%></td>
            <td><%=note.getUsername()%></td>
            <td><%=note.getPubTime()%></td>
            <td><a href="${pageContext.request.contextPath}/editShow?noteId=<%=note.getNoteId()%>">编辑</a>|<a id="delete" href="${pageContext.request.contextPath}/deleteNote?noteId=<%=note.getNoteId()%>" onclick="return deleteOne()">删除</a></td>
        </tr>
        <%
            }
        %>
        </tbody>
    </table>
    <div class="page-bar">
        <ul class="page-num-ul clearfix">
            <li>共<%=page1.getTotalData()%>条记录&nbsp;&nbsp; <%=page1.getCurrentPage()%>/<%=page1.getTotalPage()%>页</li>
                <a href="${pageContext.request.contextPath}/noteShow?currentPage=1">首页</a>
                <a href="${pageContext.request.contextPath}/noteShow?currentPage=<%=page1.getCurrentPage()-1%>">上一页</a>
                <a href="${pageContext.request.contextPath}/noteShow?currentPage=<%=page1.getCurrentPage()+1%>">下一页</a>
                <a href="${pageContext.request.contextPath}/noteShow?currentPage=<%=page1.getTotalPage()%>">尾页</a>

        </ul>
        <span class="page-go-form"><label>跳转至</label>
	     <input type="text" name="inputPage" id="inputPage" class="page-key" />页
	     <button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button>
		</span>
    </div>
</div>
</section>
<%@include file="/jsp/common/foot.jsp" %>
<script type="text/javascript">
    function deleteOne() {
        var msg = "您真的确定要删除吗?\n请确认！";
        if (confirm(msg)==true){
            return true;
        }else{
            return false;
        }
    }
    $("#btn1").click(function () {
        var $title = $("#title").val();
        $.ajax({
            type:"post",
            dataType: "json",
            data:{"title":$title},
            url:"${pageContext.request.contextPath}/fuzzyQ",
            success:function (result) {
                $("#data").empty();
                $(".page-bar").empty();
                var str = "";
                for (let i = 0; i < result.length; i++) {
                    result[i].pubTime = timeFilter(result[i].pubTime);
                    str +="<tr><td>"+result[i].noteId+
                        "</td><td><a href='${pageContext.request.contextPath}/noteView?noteId="+result[i].noteId+"'>"+result[i].title+
                        "</a></td><td>"+result[i].typename+
                        "</td><td>"+result[i].username+
                        "</td><td>"+result[i].pubTime+
                        "</td><td><a href='${pageContext.request.contextPath}/editShow?noteId="+result[i].noteId+"'>编辑</a>"+
                        "|<a href='${pageContext.request.contextPath}/deleteNote?noteId="+result[i].noteId+"'>删除</a></td></tr>"
                }
                $("#data").html(str);
            }
        })
    })
    $("#type").change(function (){
        var $typeid = $('select  option:selected').val();
        if ($typeid=="-1"){
            location.href="${pageContext.request.contextPath}/noteShow?currentPage=1"
        }else{
            $.ajax({
                type:"post",
                dataType:"json",
                url:"${pageContext.request.contextPath}/getNoteByType",
                data:{"typeid":$typeid},
                success:function(result){
                    $("#data").empty();
                    $(".page-bar").empty();
                    var str = "";
                    for (let i = 0; i < result.length; i++) {
                        result[i].pubTime = timeFilter(result[i].pubTime);
                        str +="<tr><td>"+result[i].noteId+
                            "</td><td><a href='${pageContext.request.contextPath}/noteView?noteId="+result[i].noteId+"'>"+result[i].title+
                            "</a></td><td>"+result[i].typename+
                            "</td><td>"+result[i].username+
                            "</td><td>"+result[i].pubTime+
                            "</td><td><a href='${pageContext.request.contextPath}/editShow?noteId="+result[i].noteId+"'>编辑</a>"+
                            "|<a href='${pageContext.request.contextPath}/deleteNote?noteId="+result[i].noteId+"'>编辑</a></td></tr>"
                    }
                    $("#data").html(str);
                }
            })
        }
    })
    function timeFilter (time = +new Date()) {
        const date = new Date(time + 8 * 3600 * 1000);
        return date.toJSON().substr(0, 19).replace('T', ' ');
    }

</script>